package infostroy.bookagolf.components.dialogs;

import com.academysmart.bookagolf.i18n.Messages;
import com.academysmart.bookagolf.model.Equipment;
import com.academysmart.bookagolf.model.EquipmentSetItem;
import com.academysmart.bookagolf.model.Order;
import com.academysmart.bookagolf.provider.EquipmentProvider;
import com.academysmart.bookagolf.provider.UserProvider;
import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.helpers.DateTimeHelper;
import infostroy.bookagolf.common.helpers.ValidationHelper;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * Created by Andrew on 24.02.14.
 */
public class GolfBookingTableCellDialog extends GolfBaseDialog {

	private JTable table;
	private int row;
	private JPanel mainPane;
	private JPanel orderInformationPanel;
	private JPanel accessoryInformationPanel;
	private JLabel userNameLabel;
	private JLabel playerNameLabel;
	private JLabel priceLabel;
	private JLabel receiptDateLabel;
	private JLabel orderDateLabel;
	private JLabel stateLabel;
	private JLabel idLabel;
	private JLabel paidLabel;
	private JLabel accPriceLabel;
	private JLabel additionalAccLabel;
	private JLabel accessoriesLabel;
	private JLabel accCountLabel;
	private List<Order> orders;
    private boolean orderSetted;
    private Order order;
    private static String PLAYERS_NAME = Messages.PlayerName;

    public GolfBookingTableCellDialog(String title, Color background,
			Dimension dimension, Color titleColor) {
		super(title, background, dimension, titleColor);
		initPanels();
		initLabels();

		addOrderInformationComponents();
		addAccessoryInformationComponents();
		addContentPanelComponents();

	}

	private void addContentPanelComponents() {
		mainPane.add(Box.createRigidArea(new Dimension(4, 10)));
		mainPane.add(orderInformationPanel);
		mainPane.add(Box.createRigidArea(new Dimension(4, 10)));
		mainPane.add(additionalAccLabel);
		mainPane.add(Box.createRigidArea(new Dimension(4, 10)));
		mainPane.add(accessoryInformationPanel);
		this.contentPanel.add(mainPane);
	}

	private void addAccessoryInformationComponents() {
		accessoryInformationPanel.add(accessoriesLabel);
		accessoryInformationPanel.add(accCountLabel);
	}

	private void addOrderInformationComponents() {
		orderInformationPanel.add(idLabel);
		orderInformationPanel.add(userNameLabel);
		orderInformationPanel.add(playerNameLabel);
		orderInformationPanel.add(priceLabel);
		orderInformationPanel.add(paidLabel);
		orderInformationPanel.add(accPriceLabel);
		orderInformationPanel.add(receiptDateLabel);
		orderInformationPanel.add(orderDateLabel);
		orderInformationPanel.add(stateLabel);
	}

	private void initPanels() {
		this.contentPanel.setBorder(BorderFactory.createMatteBorder(0, 15, 0,
				15, Colors.DIALOG_PANEL_BACKGROUND));

		mainPane = new JPanel();
		mainPane.setBackground(Colors.DIALOG_PANEL_BACKGROUND);
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));

		orderInformationPanel = new JPanel();
		orderInformationPanel.setBackground(Colors.DIALOG_PANEL_BACKGROUND);
		orderInformationPanel.setLayout(new BoxLayout(orderInformationPanel,
				BoxLayout.PAGE_AXIS));
		accessoryInformationPanel = new JPanel();
		accessoryInformationPanel.setLayout(new BoxLayout(
				accessoryInformationPanel, BoxLayout.PAGE_AXIS));
		accessoryInformationPanel.setBackground(Colors.DIALOG_PANEL_BACKGROUND);

		orderInformationPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

		accessoryInformationPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
	}

	private void initLabels() {
		userNameLabel = new JLabel();
        playerNameLabel = new JLabel();
        priceLabel = new JLabel();
		receiptDateLabel = new JLabel();
		orderDateLabel = new JLabel();
		stateLabel = new JLabel();

		idLabel = new JLabel();

		paidLabel = new JLabel();
		accPriceLabel = new JLabel();
		additionalAccLabel = new JLabel();
		accessoriesLabel = new JLabel();
		accCountLabel = new JLabel();
		additionalAccLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	}

	@Override
	protected void initContent() {
        Order order;
        if (!orderSetted) {
            order = orders.get(row);
        } else {
            order = this.order;
            orderSetted = false;
        }

        idLabel.setText(Messages.Order_id + ": " + order.getIdentifier());
		userNameLabel.setText(Messages.Order_user + ": "
				+ UserProvider.getUser(order.getUserId()).getFullName());
        playerNameLabel.setText(PLAYERS_NAME+ order.getUserName());
		priceLabel.setText(Messages.Order_price + ": " + order.getPrice());
		receiptDateLabel.setText(Messages.Order_createdAt
				+ ": "
				+ DateTimeHelper.convertToAppDate(order.getCreatedAt(),
						DateTimeHelper.DATE_TYPE_REST_WITH_TZ));
		orderDateLabel.setText(Messages.Order_date
				+ ": "
				+ DateTimeHelper.convertToAppDate(order.getDate(),
						DateTimeHelper.DATE_TYPE_REST));
		stateLabel.setText(Messages.Order_status + ": "
				+ Order.STATUS_MAP.get(order.getStatus()));

		paidLabel.setText(Messages.Order_paid + ": " + order.getPaid());
		accPriceLabel.setText(Messages.Order_equipmentPrice + ":  "
				+ order.getEquipmentPrice());
		additionalAccLabel.setText(Messages.Order_additionalEquipment + ": ");
		if (!ValidationHelper.isNullOrEmpty(order.getEquipmentSet())) {
			String eqStr = "";
			for (EquipmentSetItem item : order.getEquipmentSet()) {
				Equipment equipment;
				if (item.getId() != null) {
					equipment = EquipmentProvider.INSTANCE.getEquipment(item
							.getId().intValue());

					eqStr += "<html>" + Messages.Order_equipment + ": "
							+ (equipment != null ? equipment.getName() : "")
							+ "<br>" + Messages.Order_amount + ": "
							+ item.getAmount() + "</html>";
				}
			}
			accessoriesLabel.setText(eqStr);
		}
	}

	public void setDialogContent(JTable table, int row, List<Order> orders) {
		this.table = table;
		this.row = row;
		this.orders = orders;
	}

    public void setOrder(Order order) {
        this.orderSetted = true;
        this.order = order;
    }

}
